package com.hou.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hou.mapper.SanbankcMapper;
import com.hou.mapper.VipLoginMapper;
import com.hou.pojo.Sanbankc;
import com.hou.pojo.Vip;
import com.hou.service.ISanbankcService;
import com.hou.service.IVipService;
import com.hou.serviceImpl.SanbankcServiceImpl;
import com.hou.vo.CaseVip;
import com.hou.vo.JsonResult2;
import com.hzk.utils.UUIDUtil;
import com.hzk.vo.RespBean;
import com.hzk.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @auoter:jackh
 */
@RestController
@Controller
@RequestMapping("/hou")
public class vipController extends BaseController {
    @Autowired
    IVipService iVipService;
    @Autowired
    VipLoginMapper vipLoginMapper;
    @Autowired
    SanbankcMapper sanbankcService;


    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/vip/select")
    public RespBean select(@CaseVip Vip vip) {
        if (vip == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        List<Vip> select = iVipService.select();
        if (select == null) {
            return RespBean.error(RespBeanEnum.NULL_LIST_ERROR);
        }
        return RespBean.success(select);
    }

    @RequestMapping("/vip/login")
    public JsonResult2 login(Long phone, String password) {

        /*Vip login = iVipService.login(phone, password);*/
        Long byPhone = vipLoginMapper.findByPhone(phone);
        if (byPhone == null) {
            /*throw new VipLoginNotPhoneException("登录失败，手机号不存在");*/
            return new JsonResult2(400, "登录失败", null, null, null);
        }
        Vip vip = vipLoginMapper.VipLogin(phone, password);
        if (vip == null) {
            return new JsonResult2(400, "登录失败2", null, null, null);
        }

        /** 生成token令牌*/
        String token = UUIDUtil.uuid();
        // 存到Redis数据库
        // 以令牌为k，以data为value，存30分钟
        redisTemplate.opsForValue().set("VIP:" + token, vip);

        return new JsonResult2(200, "登录成功", token, vip.getUsername(), String.valueOf(vip.getPhone()));
    }

    @RequestMapping("/bank/select")
    public RespBean ban(@CaseVip Vip vip) {

        if (vip == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        List<Sanbankc> sanbankcs = sanbankcService.select();

        if (sanbankcs == null) {
            return RespBean.error(RespBeanEnum.NULL_LIST_ERROR);
        }
        return RespBean.success(sanbankcs);
    }
}



